<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Linkslistsqrcodes extends Model
{
    protected $table = 'links_lists_qrcodes';
    protected $primaryKey = 'id';

    public static function getLinksListsQrcodes($links_id)
    {
        return self::select("links_id","qrcodes_id","img_url")->where("links_id",$links_id)->where("status",1)->orderBy("id","DESC")->get();
    }

    public static function linkQrcodeLists($links_id)
    {
        return self::select("links_id","qrcodes_id","name","count_limit","scan_total","img_url","status","rate","expiration_time","scan_today")->where("links_id",$links_id)->whereIn("status",[1,2])->orderBy("id","DESC")->paginate(15);
    }

    public static function adminAllLinksListsQrcodes($links_id)
    {
        return self::select("links_id","qrcodes_id","name","count_limit","scan_total","img_url","status","rate","expiration_time")->where("links_id",$links_id)->whereIn("status",[1,2])->orderBy("id","DESC")->get();
    }

    public static function apiAllLinksListsQrcodes($links_id)
    {
        return self::select("links_id","qrcodes_id","name","count_limit","scan_total","img_url","status","rate","expiration_time")->where("links_id",$links_id)->whereIn("status",[1,2])->orderBy("id","DESC")->get();
    }

    public static function getUseAbleQrcodeCount($links_id)
    {
        return self::where("links_id",$links_id)->whereIn("status",[1,2])->count();
    }

    public static function getOnlineQrcodeCount($links_id)
    {
        return self::where("links_id",$links_id)->where("status",1)->count();
    }

    public static function linksAllListsQrcodesId($links_id)
    {
        return self::select("qrcodes_id","img_url","count_limit","expiration_time","rate","name")->where("links_id",$links_id)->whereIn("status",[1,2])->orderBy("id","DESC")->get();
    }

    public static function batchInsertLinksListsQrcodes($qrcodeLists)
    {
        return self::insert($qrcodeLists);
    }

    public static function getLinkListQrcodeByQrcodeId($qrcodes_id)
    {
        return self::where("qrcodes_id",$qrcodes_id)->first();
    }

    public static function deleteLinksListsQrcodes($qrcodes_id)
    {
        return self::where("qrcodes_id",$qrcodes_id)->update(["status"=>3]);
    }

    public static function updateLinkListQrcodeStatus($qrcodes_id,$status)
    {
        return self::where("qrcodes_id",$qrcodes_id)->update(["status"=>$status]);
    }

    public static function updateLinksListsQrcodes($qrcodes_id,$name,$img_url,$expiration_time,$rate,$count_limit)
    {
        return self::where("qrcodes_id",$qrcodes_id)->update(["name"=>$name,"img_url"=>$img_url,"expiration_time"=>$expiration_time,"rate"=>$rate,"count_limit"=>$count_limit]);
    }

    public static function updateLinksListsQrcodesScan($qrcodes_id,$scan_total,$scan_today)
    {
        return self::where("qrcodes_id",$qrcodes_id)->update(["scan_total"=>$scan_total,"scan_today"=>$scan_today]);
    }

    public static function clearLinksListsQrcodesScan()
    {
        return self::whereIn('status', [1, 2])->update(["scan_today"=>0]);
    }

    public static function addLinksListsQrcodes($links_id,$qrcodes_id,$name,$img_url,$expiration_time,$rate,$count_limit)
    {
        return self::insert(["links_id"=>$links_id,"qrcodes_id"=>$qrcodes_id,"name"=>$name,"img_url"=>$img_url,"expiration_time"=>$expiration_time,"rate"=>$rate,"count_limit"=>$count_limit,"created_at"=>time()]);
    }

    public static function randomLinksListsQrcodes($links_id)
    {
        return self::select("id","qrcodes_id","img_url")->where("links_id",$links_id)->where("status",1)->inRandomOrder()->first();
    }

    public static function getFirstLinksListsQrcode($links_id)
    {
        return self::select("links_id","qrcodes_id","img_url")->where("links_id",$links_id)->where("status",1)->orderBy("id","DESC")->first();
    }

    public static function getAllLinksListsQrcodes($links_id)
    {
        return self::select("qrcodes_id","img_url","expiration_time","count_limit","scan_total","scan_today","rate")->where("links_id",$links_id)->where("expiration_time",">",time()+1800)->where("status",1)->get();
    }

    public static function linkListQrcodeAll($links_id)
    {
        return self::select("qrcodes_id","name","img_url")->where("links_id",$links_id)->whereIn("status",[1,2])->get();
    }

    public static function updateQrcodesLink($id,$qrcode_link)
    {
        return self::where("id",$id)->update(["qrcode_link"=>$qrcode_link]);
    }
}
